package com.pzb.market.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.market.mapper.MkPanicbuyingGoodsMapper;
import com.pzb.market.domain.MkPanicbuyingGoods;
import com.pzb.market.service.IMkPanicbuyingGoodsService;

/**
 * 限时活动商品Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-12-22
 */
@Service
public class MkPanicbuyingGoodsServiceImpl implements IMkPanicbuyingGoodsService 
{
    @Autowired
    private MkPanicbuyingGoodsMapper mkPanicbuyingGoodsMapper;

    /**
     * 查询限时活动商品
     * 
     * @param id 限时活动商品主键
     * @return 限时活动商品
     */
    @Override
    public MkPanicbuyingGoods selectMkPanicbuyingGoodsById(Long id)
    {
        return mkPanicbuyingGoodsMapper.selectMkPanicbuyingGoodsById(id);
    }

    /**
     * 查询限时活动商品列表
     * 
     * @param mkPanicbuyingGoods 限时活动商品
     * @return 限时活动商品
     */
    @Override
    public List<MkPanicbuyingGoods> selectMkPanicbuyingGoodsList(MkPanicbuyingGoods mkPanicbuyingGoods)
    {
        return mkPanicbuyingGoodsMapper.selectMkPanicbuyingGoodsList(mkPanicbuyingGoods);
    }

    /**
     * 新增限时活动商品
     * 
     * @param panicbuyGoodsList 限时活动商品
     * @return 结果
     */
    @Override
    public int insertMkPanicbuyingGoods(List<MkPanicbuyingGoods> panicbuyGoodsList)
    {
        int count = 0;
        //查询最后排序号
        Integer sort = mkPanicbuyingGoodsMapper.selectLastSort(panicbuyGoodsList.get(0).getPanicId());
        if(sort ==null)sort=0;
        for (MkPanicbuyingGoods mkPanicbuyingGoods : panicbuyGoodsList) {
            //查找有无该商品
            int has = mkPanicbuyingGoodsMapper.checkExist(mkPanicbuyingGoods);
            if(has == 0){
                sort++;
                mkPanicbuyingGoods.setSort(sort);
                mkPanicbuyingGoodsMapper.insertMkPanicbuyingGoods(mkPanicbuyingGoods);
                count++;
            }
        }
        return count;
    }

    /**
     * 修改限时活动商品
     * 
     * @param mkPanicbuyingGoods 限时活动商品
     * @return 结果
     */
    @Override
    public int updateMkPanicbuyingGoods(MkPanicbuyingGoods mkPanicbuyingGoods)
    {
        return mkPanicbuyingGoodsMapper.updateMkPanicbuyingGoods(mkPanicbuyingGoods);
    }

    /**
     * 批量删除限时活动商品
     * 
     * @param ids 需要删除的限时活动商品主键
     * @return 结果
     */
    @Override
    public int deleteMkPanicbuyingGoodsByIds(Long[] ids)
    {
        MkPanicbuyingGoods goods = selectMkPanicbuyingGoodsById(ids[0]);
        int count = mkPanicbuyingGoodsMapper.deleteMkPanicbuyingGoodsByIds(ids);
        //对删除后的数据重新排序
        MkPanicbuyingGoods q = new MkPanicbuyingGoods();
        q.setPanicId(goods.getPanicId());
        List<MkPanicbuyingGoods> panicGoodsList = selectMkPanicbuyingGoodsList(q);
        int sort = 1;
        for (MkPanicbuyingGoods panicbuyingGoods : panicGoodsList) {
            panicbuyingGoods.setSort(sort);
            updateMkPanicbuyingGoods(panicbuyingGoods);
            sort++;
        }
        return count;
    }

    @Override
    public int moveUpGoods(MkPanicbuyingGoods mkPanicbuyingGoods) {
        int count=0;
        //查询上一个
        MkPanicbuyingGoods q = new MkPanicbuyingGoods();
        q.setPanicId(mkPanicbuyingGoods.getPanicId());
        q.setSort(mkPanicbuyingGoods.getSort()-1);
        List<MkPanicbuyingGoods> goodsList=mkPanicbuyingGoodsMapper.selectMkPanicbuyingGoodsList(q);
        if (goodsList.size()>0) {
            mkPanicbuyingGoods.setSort(mkPanicbuyingGoods.getSort()-1);
            count = mkPanicbuyingGoodsMapper.updateMkPanicbuyingGoods(mkPanicbuyingGoods);
            MkPanicbuyingGoods goods =goodsList.get(0);
            goods.setSort(goods.getSort()+1);
            mkPanicbuyingGoodsMapper.updateMkPanicbuyingGoods(goods);
        }
        return count;
    }

    @Override
    public int moveDownGoods(MkPanicbuyingGoods mkPanicbuyingGoods) {
        int count=0;
        //查询上一个
        MkPanicbuyingGoods q = new MkPanicbuyingGoods();
        q.setPanicId(mkPanicbuyingGoods.getPanicId());
        q.setSort(mkPanicbuyingGoods.getSort()+1);
        List<MkPanicbuyingGoods> goodsList=mkPanicbuyingGoodsMapper.selectMkPanicbuyingGoodsList(q);
        if (goodsList.size()>0) {
            mkPanicbuyingGoods.setSort(mkPanicbuyingGoods.getSort()+1);
            count = mkPanicbuyingGoodsMapper.updateMkPanicbuyingGoods(mkPanicbuyingGoods);
            MkPanicbuyingGoods goods =goodsList.get(0);
            goods.setSort(goods.getSort()-1);
            mkPanicbuyingGoodsMapper.updateMkPanicbuyingGoods(goods);
        }
        return count;
    }

}
